// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-16

package services

import (
	"context"
	"encoding/json"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/http/models"
	"server/app/kind"
)

// Add
// 添加日志.
func (o *ArcadeLogsService) Add(ctx context.Context, arcade *models.Arcades, admin *models.ArcadeAdmins, operation kind.ArcadeAdminOperation, content string, params ...any) (model *models.ArcadeLogs, err error) {
	x := &models.ArcadeLogs{
		ArcadeId:  arcade.Id,
		UserId:    admin.UserId,
		Nickname:  admin.Nickname,
		Operation: operation,
		Content:   content,
	}

	if len(params) > 0 && params[0] != nil {
		if b, e := json.Marshal(params[0]); e == nil {
			x.Params = string(b)
		}
	}

	return o.Dao.AddByStruct(ctx, x)
}

// Paging
// 日志分页.
func (o *ArcadeLogsService) Paging(ctx context.Context, data map[string]any, page, size int) (list []*models.ArcadeLogs, total int64, err error) {
	var sess *db.Session
	if sess, err = o.Dao.Slaver(ctx); err == nil {
		if total, err = sess.Where(data).Count(models.ArcadeLogs{}); err == nil && total > 0 {
			err = sess.Where(data).OrderBy("created_at DESC").Limit(size, (page-1)*size).Find(&list)
		}
	}
	return
}
